//
//  YAYIInterPayController.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/4.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYIInterPayController.h"
#import "YAYIWaitPayController.h"
#import "YAYIDidPayController.h"
#import "YAYIRefundsController.h"
#import "YAYIApilyListController.h"

@interface YAYIInterPayController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIView *titlesView;
@property (nonatomic, weak) UILabel *indicatorLabel;

@property (nonatomic, weak) UIButton *selectedBtn;

@property (nonatomic, strong) NSMutableArray *btnArray;

@end


@implementation YAYIInterPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"诊间收费";
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.btnArray = [NSMutableArray array];
    
    [self setupNav];
    [self addChildVcs];
    [self setupScrollView];
    [self setupTitlesView];
    [self setupIndicator];
}


-(void)setupNav
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"收费" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 60, 30);
    [button addTarget:self action:@selector(appily:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:font18];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
}


-(void)setupScrollView
{

    UIScrollView *scrollView = [UIScrollView  new];
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    scrollView.contentSize = CGSizeMake(SCREEN_width * 3, 0);
    
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


-(void)setupTitlesView
{
    
    NSArray *titlesAry = @[@"待支付",@"已付帐单",@"退款"];
    
    UIView *titlesView = [UIView  new];
    titlesView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;

    [self.titlesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.mas_equalTo(64);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 44));
    }];
    
    CGFloat width = SCREEN_width / 3.0;
    for (int i = 0; i < titlesAry.count; i ++) {
        
        UIButton *button = [UIButton  buttonWithType:UIButtonTypeCustom];
        [button setTitle:titlesAry[i] forState:UIControlStateNormal];
        
        [button setTitleColor:YAYIColor(122, 120, 120) forState:UIControlStateNormal];
        [button setTitleColor:YAYIColor(83, 165, 243) forState:UIControlStateDisabled];
        
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:font17];
        [button addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView  addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(i * width));
            make.top.mas_equalTo(self.titlesView.mas_top);
            make.size.mas_equalTo(CGSizeMake(width, 44));
        }];
        
        if (i == 0) {
            button.enabled = NO;
            self.selectedBtn = button;
        }
        
        [self.btnArray  addObject:button];
    }
    
}


-(void)setupIndicator
{

    UILabel *indicatorLabel = [UILabel new];
    indicatorLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [self.titlesView addSubview:indicatorLabel];
    self.indicatorLabel = indicatorLabel;
    [self.indicatorLabel  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.mas_equalTo(self.titlesView.mas_bottom).with.offset(-2);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width /3.0 - 30, 2));
    }];
}


- (void)addChildVcs
{
    YAYIWaitPayController *vc1 = [[YAYIWaitPayController alloc] init];
    [self addChildViewController:vc1];
    
    YAYIDidPayController *vc2 = [[YAYIDidPayController alloc] init];
    [self addChildViewController:vc2];
    
    YAYIRefundsController *vc3 = [[YAYIRefundsController alloc] init];
    [self addChildViewController:vc3];
}


#pragma mark - 点击事件
-(void)selectAction:(UIButton *)sender
{
    
    // 修改按钮状态
    self.selectedBtn.enabled = YES;
    sender.enabled = NO;
    self.selectedBtn = sender;
    
    // 动画
    //    [UIView animateWithDuration:0.25 animations:^{
    //        self.indicatorLabel.centerX = sender.centerX;
    //    }];
    
    // 滚动
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = sender.tag * self.scrollView.width;
    [self.scrollView setContentOffset:offset animated:YES];
    
}


#pragma mark - 代理方法
-(void)appily:(UIButton *)sender{
    
    YAYIApilyListController *apily =  [YAYIApilyListController new];
    apily.title = @"杭州市西湖区";
    [self.navigationController pushViewController:apily animated:YES];
}


#pragma mark - 代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat with = SCREEN_width / 3.0;
    CGFloat w = (with/SCREEN_width)*scrollView.contentOffset.x;
    self.indicatorLabel.x = w + 15;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x /SCREEN_width;
    [self selectAction:self.btnArray[index]];
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_width;
    
    // 取出子控制器
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
    vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
    
    YAYILog(@"vc class:%@", [vc class]);
    
    [scrollView addSubview:vc.view];
}


@end
